/**
 * 常用方法封装 请求，文件上传等
 * @author echo.
 **/

const tui = {
  //去登录页面
  linkToLogin() {
    if (uni.getStorageSync("profile")) {
      const profile = JSON.parse(uni.getStorageSync("profile"));

      if (profile.access_token) {
        return profile;
      } else {
        uni.reLaunch({
          url: "/pagesA/login/login",
        });
      }
    } else {
      uni.reLaunch({
        url: "/pagesA/login/login",
      });
    }
  },
  //接口地址
  interfaceUrl: function () {
	return "http://api.cag2024.com/api"
    // return "http://www.cag2024.com/api";
    //return 'https://test.thorui.cn'
    //return 'https://uat.thorui.cn'
    // return 'https://prod.thorui.cn'
  },
  toast: function (text, duration, success) {
    uni.showToast({
      // #ifndef MP-ALIPAY
      duration: duration || 2000,
      // #endif
      title: text || "出错啦~",
      icon: success ? "success" : "none",
    });
  },
  modal: function (
    title,
    content,
    showCancel,
    callback,
    confirmColor,
    confirmText
  ) {
    uni.showModal({
      title: title || "提示",
      content: content,
      showCancel: showCancel,
      cancelColor: "#555",
      confirmColor: confirmColor || "#5677fc",
      confirmText: confirmText || "确定",
      success(res) {
        if (res.confirm) {
          callback && callback(true);
        } else {
          callback && callback(false);
        }
      },
    });
  },
  isAndroid: function () {
    const res = uni.getSystemInfoSync();
    return res.platform.toLocaleLowerCase() == "android";
  },
  isPhoneX: function () {
    const res = uni.getSystemInfoSync();
    let iphonex = false;
    let models = [
      "iphonex",
      "iphonexr",
      "iphonexsmax",
      "iphone11",
      "iphone11pro",
      "iphone11promax",
    ];
    const model = res.model.replace(/\s/g, "").toLowerCase();
    if (models.includes(model)) {
      iphonex = true;
    }
    return iphonex;
  },
  constNum: function () {
    let time = 0;
    // #ifdef APP-PLUS
    time = this.isAndroid() ? 300 : 0;
    // #endif
    return time;
  },
  delayed: null,
  loadding: false,
  showLoading: function (title, mask = true) {
    uni.showLoading({
      mask: mask,
      title: title || "请稍候...",
    });
  },
  /**
   * 请求数据处理
   * @param string url 请求地址
   * @param string method 请求方式
   *  GET or POST
   * @param {*} postData 请求参数
   * @param bool isDelay 是否延迟显示loading
   * @param bool isForm 数据格式
   *  true: 'application/x-www-form-urlencoded'
   *  false:'application/json'
   * @param bool hideLoading 是否隐藏loading
   *  true: 隐藏
   *  false:显示
   */
  request: async function (
    url,
    method,
    header,
    postData,
    isDelay,
    hideLoading = true
  ) {
    //接口请求
    tui.loadding && uni.hideLoading();
    tui.loadding = false;
    if (!hideLoading) {
      if (isDelay) {
        tui.delayed = setTimeout(() => {
          tui.loadding = true;
          tui.showLoading();
        }, 1000);
      } else {
        tui.loadding = true;
        tui.showLoading();
      }
    }

    return new Promise((resolve, reject) => {
      uni.request({
        url: tui.interfaceUrl() + url,
        data: postData,
        header: {
          Authorization: tui.getToken(),
          ...header,
        },
        method: method, //'GET','POST'
        dataType: "json",
        success: (res) => {
          clearTimeout(tui.delayed);
          tui.delayed = null;
          if (tui.loadding && !hideLoading) {
            uni.hideLoading();
          }
          resolve(res.data);
        },
        fail: (res) => {
          clearTimeout(tui.delayed);
          tui.delayed = null;
          tui.toast("网络不给力，请稍后再试~");
          uni.reLaunch({
            url: "/pagesA/login/login",
          });
          reject(res);
        },
      });
    });
  },
  /**
   * 上传文件
   * @param string url 请求地址
   * @param string src 文件路径
   */
  uploadFile: function (url, src) {
    tui.showLoading();
    return new Promise((resolve, reject) => {
      const uploadTask = uni.uploadFile({
        url: tui.interfaceUrl() + url,
        filePath: src,
        name: "imageFile",
        header: {
          Authorization: tui.getToken(),
        },
        formData: {
          // sizeArrayText:""
        },
        success: function (res) {
          uni.hideLoading();
          let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}");
          if (d.code % 100 == 0) {
            //返回图片地址
            let fileObj = d.data;
            resolve(fileObj);
          } else {
            that.toast(res.msg);
          }
        },
        fail: function (res) {
          reject(res);
          that.toast(res.msg);
        },
      });
    });
  },
  tuiJsonp: function (url, callback, callbackname) {
    // #ifdef H5
    window[callbackname] = callback;
    let tuiScript = document.createElement("script");
    tuiScript.src = url;
    tuiScript.type = "text/javascript";
    document.head.appendChild(tuiScript);
    document.head.removeChild(tuiScript);
    // #endif
  },
  //设置用户信息
  setUserInfo: function (mobile, token) {
    //uni.setStorageSync("thorui_token", token)
    uni.setStorageSync("thorui_mobile", mobile);
  },
  //获取token
  getToken() {
    return uni.getStorageSync("thorui_token");
  },
  //判断是否登录
  isLogin: function () {
    return uni.getStorageSync("thorui_mobile") ? true : false;
  },
  //跳转页面，校验登录状态
  href(url, isVerify) {
    if (isVerify && !tui.isLogin()) {
      uni.navigateTo({
        url: "/pages/common/login/login",
      });
    } else {
      uni.navigateTo({
        url: url,
      });
    }
  },
};

export default tui;
